//
//  RBCartListCell.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/18.
//

#import "RBCartListCell.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBPager.h"
#import "RanTool.h"
#import "RBGradientView.h"
#import <UIImage+YYAdd.h>
#import "RBTextField.h"
#import "RBCartSpecVC.h"

@interface RBCartListCell()<UITextFieldDelegate>

@property(nonatomic, strong)UIView *containerView;
@property(nonatomic, strong)UIView *shopView;
@property(nonatomic, strong)UIImageView *shopSelImgView;
@property(nonatomic, strong)UILabel *shopNameL;

@property(nonatomic, strong)RBCellModel *cellModel;

@end

@implementation RBCartListCell

#pragma mark - Event

- (void)shopSelImgViewTapped
{
    if (self.cellModel.eventBlock1)
    {
        self.cellModel.eventBlock1(RBCartListCellActionShopSelectEvent, 0, self.cellModel.cellData);
    }
}

- (void)goodsSelImgViewTapped: (UITapGestureRecognizer *)ges
{
    if (self.cellModel.eventBlock1)
    {
        self.cellModel.eventBlock1(RBCartListCellActionGoodsSelectEvent, ges.view.tag, self.cellModel.cellData);
    }
}

- (void)minusBtnTapped: (UIGestureRecognizer *)ges
{
    if (self.cellModel.eventBlock1)
    {
        self.cellModel.eventBlock1(RBCartListCellActionCountMinusEvent, ges.view.tag, self.cellModel.cellData);
    }
}

- (void)plusBtnTapped: (UIGestureRecognizer *)ges
{
    if (self.cellModel.eventBlock1)
    {
        self.cellModel.eventBlock1(RBCartListCellActionCountPlusEvent, ges.view.tag, self.cellModel.cellData);
    }
}

- (void)specViewTapped
{
    RBCartSpecVC *vc = RBCartSpecVC.new;
    RBCartModel *model = self.cellModel.cellData;
    vc.cartSpecArray = model.spec_key_name;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITextFieldDelegate

- (void)textFieldDidEndEditing:(RBTextField *)textField
{
    if (!textField.text.length)
    {
        textField.text = [NSString stringWithFormat:@"%zd", textField.minInt];
    }
    if (self.cellModel.eventBlock1)
    {
        self.cellModel.eventBlock1(RBCartListCellActionCountChangedEvent, 0, self.cellModel.cellData);
    }
}

#pragma mark - Setter

- (void)setCellModel:(RBCellModel *)cellModel
{
    _cellModel = cellModel;
    RBCartModel *model = cellModel.cellData;
    [self.containerView view_clear];
    [self.containerView view_addView:self.shopView withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
    self.shopNameL.text = model.store_name;
    self.shopSelImgView.image = model.selected? [self selImg]: [self norImg];
    self.shopSelImgView.hidden = YES;
//    for (NSInteger i = 0; i < model.items.count; i++)
//    {
        [self.containerView view_addView:[self goodsViewWithIndex:0] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top: 0 bottom:Zoom(20) left:0 right:0 centerX:0];
//    }
}

#pragma mark - Getter

- (UIView *)containerView
{
    if (!_containerView)
    {
        self.containerView = ({
            UIView *view = UIView.new;
            view.backgroundColor = UIColor.whiteColor;
            [view rbSetCornerRadius:Zoom(8)];
            [self.contentView addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(Zoom(350));
                make.edges.mas_equalTo(0);
            }];
            view;
        });
    }
    return _containerView;
}

- (UIView *)shopView
{
    if (!_shopView)
    {
        self.shopView = ({
            UIView *view = UIView.new;
            
            [view h_view_addView:({
                self.shopSelImgView = [RanTool imgViewWithMode:0 radius:0 imgName:@""];
                [self.shopSelImgView rbAddTapGesWithTarget:self action:@selector(shopSelImgViewTapped)];
                [self.shopSelImgView setResponseArea:UIEdgeInsetsMake(Zoom(10), Zoom(10), Zoom(10), Zoom(10))];
                self.shopSelImgView;
            }) withHeight:Zoom(15) width:Zoom(15) top:Zoom(16) bottom:Zoom(16) left:Zoom(8) right:0 centerY:NO];
            
            [view h_view_addView:({
                self.shopNameL = [RanTool labelWithFontSize:Zoom(16) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:@""];
                self.shopNameL;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(12) right:Zoom(12) centerY:NO];
            
            view;
        });
    }
    return _shopView;
}

- (UIView *)goodsViewWithIndex: (NSInteger)index
{
    __weak typeof(self) weakSelf = self;
    UIView *view = UIView.new;
    
    RBCartModel *model = self.cellModel.cellData;
    
    [view view_addView:({
        UIView *view = UIView.new;
        [view h_view_addView:({
            UIImageView *view = [RanTool imgViewWithMode:0 radius:0 imgName:@""];
            view.image = model.selected? [self selImg]: [self norImg];
            view.tag = index;
            [view rbAddTapGesWithTarget:self action:@selector(goodsSelImgViewTapped:)];
            [view setResponseArea:UIEdgeInsetsMake(Zoom(10), Zoom(10), Zoom(10), Zoom(10))];
            view;
        }) withHeight:Zoom(15) width:Zoom(15) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:Zoom(8) right:0 centerY:YES];
        [view h_view_addView:[RanTool imgViewWithMode:2 radius:Zoom(5) imgName:model.picurl] withHeight:Zoom(82) width:Zoom(82) top:0 bottom:0 left:Zoom(12) right:CGFLOAT_MIN centerY:NO];
        [view h_view_addView:({
            UIView *view = UIView.new;
            //名称
            [view view_addView:({
                UILabel *view = [RanTool labelWithFontSize:Zoom(16) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:model.good_name];
                view.numberOfLines = 2;
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
            //价格 & 属性
            [view view_addView:({
                UIView *view = UIView.new;
        
                [view h_view_addView:({
                    UILabel *view = UILabel.new;
                    view.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
                        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(11)] color:[RBThemeConfig priceRedColor] string:@"¥"];
                        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(15)] color:[RBThemeConfig priceRedColor] string:model.good_price];
                    }];
                    view;
                }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:0 right:CGFLOAT_MIN centerY:YES];
                
                [view h_view_addView:({
                    UIView *view = UIView.new;
                    [view rbAddTapGesWithTarget:self action:@selector(specViewTapped)];
                    view.backgroundColor = [RBThemeConfig bgGrayColor];
                    [view rbSetCornerRadius:Zoom(11)];
                    [view h_view_addView:({
                        UILabel *view = [RanTool labelWithFontSize:Zoom(11) weight:0 color:[RBThemeConfig textGrayColor] align:0 text:@"查看规格"];
                        [view addPriority:1];
                        view;
                    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(9) right:0 centerY:0];
                    [view h_view_addView:[RanTool imgViewWithMode:0 radius:0 imgName:@"paramDown"] withHeight:Zoom(3) width:Zoom(8) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:Zoom(8) right:Zoom(9) centerY:YES];
                    view;
                }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(12) right:CGFLOAT_MIN centerY:0];
                
                view;
            }) withHeight:Zoom(22) width:CGFLOAT_MIN top:Zoom(12) bottom:0 left:0 right:0 centerX:0];

            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(12) right:Zoom(8) centerY:YES];
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:NO];
    
    return view;
}

- (UIImage *)selImg
{
    return [UIImage imageNamed:@"addressListDefault"];
}

- (UIImage *)norImg
{
    return [UIImage imageNamed:@"unsel"];
}

@end
